package com.wushi.lenovo.asia5b.app_ui.voucher.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mvp.base.BaseMvpFragment;
import com.mvp.base.model.ResultModel;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_config.CommonConstant;
import com.wushi.lenovo.asia5b.app_di.APPComponentFactory;
import com.wushi.lenovo.asia5b.app_di.voucher.mvpview.VoucherFragmentView;
import com.wushi.lenovo.asia5b.app_di.voucher.presenter.VoucherFragmentPresenter;
import com.wushi.lenovo.asia5b.app_model.voucher.VoucherModel;
import com.wushi.lenovo.asia5b.app_model.voucher.VoucherType;
import com.wushi.lenovo.asia5b.app_ui.other.dialog.VoucherDetailDialog;
import com.wushi.lenovo.asia5b.app_ui.voucher.adapter.VoucherAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by zhangyuncai on 2019/1/7.
 */
public class VoucherFragment extends BaseMvpFragment<VoucherFragmentView, VoucherFragmentPresenter>
        implements VoucherFragmentView {
    @BindView(R.id.recyclerview_voucher)
    RecyclerView mRecyclerView;
    private VoucherAdapter mAdapter;
    private final List<VoucherModel> mDatas = new ArrayList<>();
    ;
    VoucherType voucherType;
    private View emptyView;

    @Override
    protected void constructComponent() {
        APPComponentFactory.getAppBaseComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_voucher;
    }

    public static Fragment newInstance(Context context, VoucherType voucherType) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(CommonConstant.VoucherType, voucherType);
        return Fragment.instantiate(context, VoucherFragment.class.getName(), bundle);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        voucherType = (VoucherType) getArguments().getSerializable(CommonConstant.VoucherType);
        initView();
        initEvent();
    }

    @Override
    protected void lazyFetchData() {
        initData();
    }

    @Override
    protected void initView() {
        mAdapter = new VoucherAdapter(mDatas,voucherType,getContext());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(mAdapter);

        emptyView = View.inflate(getContext(), R.layout.empty_voucher, null);
    }

    @Override
    protected void initData() {
        getPresenter().getVouchers(voucherType);
    }

    @Override
    protected void initEvent() {
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (voucherType == VoucherType.unused) {
                    VoucherModel voucherModel = mAdapter.getItem(position);
                    VoucherDetailDialog dialog = new VoucherDetailDialog(getContext(), voucherModel);
                    dialog.show();
                }
            }
        });
    }

    @Override
    public void getVouchersResult(ResultModel<List<VoucherModel>> resultModel, VoucherType voucherType) {
        mDatas.clear();
        if (resultModel.isSuccess() && resultModel.getData() != null) {
            mDatas.addAll(resultModel.getData());
        }
        mAdapter.setEmptyView(emptyView);
        mAdapter.notifyDataSetChanged();

    }
}
